package pgpchat;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

import pgpchat.pgp.PGPActor;

public class AwaytingCliente extends SwingWorker<String, Void> {

	AguardandoDialog d;
	ServerSocket servidor;
	Socket client;
	PGPActor clientPGP;

	public AwaytingCliente(AguardandoDialog d, ServerSocket servidor, Socket client, PGPActor clientPGP) {
		this.d = d;
		this.servidor = servidor;
		this.client = client;
		this.clientPGP = clientPGP;
	}

	@Override
	protected String doInBackground() throws InterruptedException {
		try {
			client = servidor.accept();
			System.out.println("O cliente se conectou ao servidor!");

			PGPHelper.sendKey(clientPGP.getPubKey(), client);
			clientPGP.setReceiverPubKey(PGPHelper.receivePubKey(client));

		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "Erro ao aguardar o cliente");
			e.printStackTrace();
		}
		return "";
	}

	@Override
	protected void done() {
		d.dispose();
	}

	public Socket getClient() {
		return this.client;
	}

}
